// pages/detail/childCpns/w-product-specifications/w-product-specifications.js
import {
  DetailShopCartClass
} from "../detailClass"

import * as Constant from "../../../../common/constant"

const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isShow: {
      type: Boolean,
      value: false
    },
    iid: {
      type: String,
      value: ''
    },
    title: {
      type: String,
      value: ''
    },
    desc: {
      type: String,
      value: ''
    },
    skuInfo: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    topImg: null,
    topPrice: null,
    currentStyleId: null,
    currentSizeId: null,
    styleName: null,
    size: null,
    xdSkuId: null,
    stockCount: 0,
    purchaseCount: 1,
    msgIndex: null
  },
  observers: {
    'skuInfo'(skuInfo) {
      if (Object.keys(skuInfo).length > 0) {
        this.setData({
          topImg: skuInfo.skus[0].img,
          topPrice: (skuInfo.skus[0].nowprice / 100).toFixed(2),
          stockCount: skuInfo.totalStock
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    commodityStyleClick(event) {
      let dataset = event.currentTarget.dataset;
      let index = dataset.index;
      let indey = dataset.indey;
      let prop = this.properties.skuInfo.props[index];
      prop.list.forEach((item, ind) => {
        if (ind === indey) {
          if (prop.label === '颜色:') {
            this.setData({
              styleName: item.name,
              currentStyleId: item.styleId
            })
          }
          if (prop.label === '尺码:') {
            this.setData({
              size: item.name,
              currentSizeId: item.sizeId
            })
          }
        }
      })
      let findItem = this.properties.skuInfo.skus.find(item => item.styleId === this.data.currentStyleId && item.sizeId === this.data.currentSizeId);
      if (findItem) {
        this.setData({
          xdSkuId: findItem.xdSkuId,
          topImg: findItem.img,
          topPrice: (findItem.nowprice / 100).toFixed(2),
          stockCount: findItem.stock
        })
      }
    },
    handleDecremental() {
      this.setData({
        purchaseCount: --this.data.purchaseCount
      })
    },
    handleIncremental() {
      this.setData({
        purchaseCount: ++this.data.purchaseCount
      })
    },
    submitBtnClick() {
      if (!this.data.currentStyleId) {
        return wx.showToast({
          title: '请选择一个颜色',
          icon: 'none'
        })
      }
      if (!this.data.currentSizeId) {
        return wx.showToast({
          title: '请选择一个尺码',
          icon: 'none'
        })
      }
      let detailShopCartObject = new DetailShopCartClass(this.data.iid, this.data.title, this.data.styleName, this.data.size, this.data.topImg, this.data.topPrice, this.data.purchaseCount, this.data.xdSkuId);
      app.addToShopCart(detailShopCartObject).then(() => {
        wx.showToast({
          title: '添加购物车成功',
          icon: 'none',
          success: () => {
            this.setData({
              isShow: false
            })
          }
        })
      });
    }
  }
})